package com.zzyl.controller.security;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(path = "/page/{pageNum}/{pageSize}")
    @ApiOperation("用户分页查询")
    public ResponseResult<PageResponse<UserVo>> selectUserPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @RequestBody UserDto userDto){
        PageResponse<UserVo> pageResponse = userService.selectUserPage(pageNum,pageSize,userDto);
        return ResponseResult.success(pageResponse);
    }

    @PutMapping
    @ApiOperation("新增用户")
    public ResponseResult<PageResponse<Void>> addUser(@RequestBody UserDto userDto){
        userService.addUser(userDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("修改用户")
    public ResponseResult<Void> updateUser(@RequestBody UserDto userDto){
        userService.updateUser(userDto);
        return ResponseResult.success();
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用/禁用用户")
    public ResponseResult<Void> updateUserDataState(@PathVariable("id") Long id, @PathVariable("status") String status){
        userService.updateUserDataState(id,status);
        return ResponseResult.success();
    }

    @DeleteMapping(path = "/remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult<Void> deleteUser(@PathVariable("userIds") String userIds){
        if (userService.deleteUser(userIds)){
            return ResponseResult.success();
        }else {
            return ResponseResult.error("启用状态下不可删除");
        }
    }

    @PostMapping(path = "/reset-passwords/{userId}")
    @ApiOperation("重置密码")
    public ResponseResult<Void> resetPasswords(@PathVariable("userId") Long userId){
        userService.resetPasswords(userId);
        return ResponseResult.success();
    }

    @PostMapping(path = "/list")
    @ApiOperation("可用用户列表")
    public ResponseResult<List<UserVo>> findList(@RequestBody UserDto userDto){
        return ResponseResult.success(userService.selectAllUser(userDto));
    }
}
